define(["jquery","bbcare","comm/datatable/datatable","bld/app/course/courseList.ui"],function($,$bb,$datatable,$ui){
    var servers = {};
    var URL = $bb.baseUrl("blood");

    /**
     * 获取所有方案分类
     */
    servers.ajaxQueryAllScheme = function(callback) {
        $bb.ajax({
            url: URL + "schemeClass/queryAllSchemeClass",
            success: function(data) {
                if (data.success) {
                    callback(data.data);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "获取所有方案分类出错！");
                }
            }
        });
    }
    /**
     * 查询疗程分类
     */
    servers.ajaxCourseClass = function(callback) {
        $bb.ajax({
            url: URL + "courseClass/queryCourseLib",
            data: JSON.stringify({
                data: {
                    "queryCriteria":{}
                }
            }),
            success: function(data) {
                if (data.success) {
                    callback(data.data);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "查询疗程分类出错！");
                }
            }
        });
    }
    /**
     * 添加疗程
     */
    servers.ajaxAdd = function(params,callback) {
        $bb.ajax({
            url: URL + "course/addCourse",
            data: JSON.stringify({
                data: params
            }),
            success: function(data) {
                if (data.success) {
                    callback(data.data);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "添加疗程出错！");
                }
            }
        });
    }
    /**
     * 删除疗程
     */
    servers.ajaxDeleteCourse = function(params,callback) {
        $bb.ajax({
            url: URL + "course/deleteCourse",
            data: JSON.stringify({
                data: params
            }),
            success: function(data) {
                if (data.success) {
                    callback(data.data);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "删除疗程出错！");
                }
            }
        });
    }
    /**
     * 复制疗程
     */
    servers.ajaxCopyCourse = function(params,callback) {
        $bb.ajax({
            url: URL + "course/copyCourse",
            data: JSON.stringify({
                data: params
            }),
            success: function(data) {
                if (data.success) {
                    callback(data.data);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "复制疗程出错！");
                }
            }
        });
    }

    /**
     * 复制疗程库成功后的回调函数
     */
    var ajaxCopyCourse = function(data) {
        $bb.toastShow('复制成功！');
        $datatable.table.api().ajax.reload();
    }

    /**
     * 复制疗程
     */
    var onCopyCourse = function(){
        var data = $(this).data("data");
        var doctorName = JSON.parse($bb.getSessionStorage("account")).alias;
        var channelId = $bb.getChannelId();
        var params = {
            "oprInfo":{
                "opCode": "2053",
                "opType": "3",
                "opNote": "【" + doctorName + "】医生复制疗程【" + data.course_name + "】",
                "channelType": "2",
                "channelId": channelId
            },
            "courseId":data.id,
        };
        servers.ajaxCopyCourse(params,ajaxCopyCourse);
    }

    /**
     * 删除疗程库成功后的回调函数
     */
    var ajaxDeleteCourse = function(data) {
        $bb.toastShow('删除成功！');
        $datatable.table.api().ajax.reload();
    }

    /**
     * 删除疗程
     */
    var onDelCourse = function(){
        var data = $(this).data("data");
        var doctorName = JSON.parse($bb.getSessionStorage("account")).alias;
        var channelId = $bb.getChannelId();
        var params = {
            "oprInfo":{
                "opCode": "2052",
                "opType": "3",
                "opNote": "【" + doctorName + "】医生删除疗程【" + data.course_name + "】",
                "channelType": "2",
                "channelId": channelId
            },
            "courseId":data.id,
            "courseVersion":data.course_version
        };
        servers.ajaxDeleteCourse(params,ajaxDeleteCourse);
    }

    /**
     * 出参
     */
    var inParams = function(args) {
        var courseName = $("#courseName").val();
        var groupValue = $("#group").val();
        var schemeId = $("#classify").val();
        var data = JSON.stringify({
            data: {
                "args": JSON.stringify(args),
                "queryCriteria": {
                    "courseName": courseName,
                    "groupType":"0",
                    "groupValue": groupValue,
                    "schemeId": schemeId
                }
            }
        });
        return data;
    }
    /**
     * 出参
     */
    var outParams = function(data) {
        return data.rows;
    }
    var groupValue = function(e){
        return e.group_value == "00" ? "不随机" : e.group_value == "01" ? "随机A组" : e.group_value == "02" ? "随机B组" : e.group_value == "03" ? "随机D组" : e.group_value == "04" ? "随机I组" : "";
    }
    var courseType = function(e) {
        return e.course_type == '01' ? '是' : '否';
    }
    var status = function(e) {
        return e.status == '1' ? '已发布' : '未发布';
    }
    var columnsData = function() {
        var columnsArr = [
            { "data": 'course_name' },
            { "data": 'create_time' },
            { "data": groupValue },
            { "data": courseType },
            { "data": 'class_name' },
            { "data": status },
            {
                "data": null,
                "className": "operate"
            }
        ];
        return columnsArr;
    }
    /**
     * 行绘制回调函数
     */
    var rowCallback = function(row, data, index) {
        var h = '<div class="operate-box"><span class="color-green cursor-pointer courseList-edit">查看</span><span class="color-green cursor-pointer ml8 courseList-del">删除</span><span class="color-green cursor-pointer ml8 courseList-copy">复制</span></div>';
        $('td.operate', row).html(h);
        /* 查看疗程信息 */
        $("td.operate .courseList-edit", row).unbind("click");
        $("td.operate .courseList-edit", row).click(function() {
            var params = { 'courseName': data.course_name, 'id': data.id, 'courseVersion': data.course_version, 'status': data.status };
            $bb.openPage('bld_courseEdit',params);
        });
        /* 删除疗程 */
        $("td.operate .courseList-del", row).unbind("click");
        $("td.operate .courseList-del", row).data("data",data);
        $("td.operate .courseList-del", row).click(onDelCourse);
        /* 复制疗程 */
        $("td.operate .courseList-copy", row).unbind("click");
        $("td.operate .courseList-copy", row).data("data",data);
        $("td.operate .courseList-copy", row).click(onCopyCourse);
    }
    /**
     * 初始化完成之后的回调函数
     */
    var initComplete = function(setting, json) {
        var id = $(".layout-bottom table").attr("id");
        $("#" + id).prev("div.row").remove();
        // 搜索 
        $("#searchBtn").click(function() {
            $datatable.table.api().ajax.reload(); // 出发dt的重新加载数据的方法
            var args = $datatable.table.api().ajax.params(); //获取dt请求参数 
        });
        // 初始化所有工具提示
        $(function() { $("[data-toggle='tooltip']").tooltip(); });
    }

    /**
     * 获取datatable里的options
     */
    servers.getOptions = function() {
        var sessionId = $bb.getSessionStorage("sessionId");
        var options = {
            "ajax": {
                "url": URL + "course/queryCourse?sessionId=" + sessionId,
                "data": inParams,
                "dataSrc": outParams,
                error: function(jqXHR, textStatus, errorMsg) {
                    $bb.toastShow("没有查询到疗程，请再试一次！", 5000);
                }
            },
            "columns": columnsData(),
            "rowCallback": rowCallback,
            "initComplete": initComplete
        };
        return options;
    }

    return servers;
})